package com.ai.wxy.spring.user.service;

import com.ai.spring.data.jpa.dao.IJpaDao;
import com.ai.wxy.spring.service.api.BaseService;
import com.ai.wxy.spring.service.api.dto.DefaultServiceParam;
import com.ai.wxy.spring.service.api.dto.DefaultServiceResult;
import com.ai.wxy.spring.service.api.dto.ServiceResultList;
import com.ai.wxy.spring.user.convert.UserVoConvert;
import com.ai.wxy.spring.user.domain.User;
import com.ai.wxy.spring.user.vo.UserQueryVo;
import com.ai.wxy.spring.user.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;

/**
 * 测试
 *
 * @author 石头
 * @Date 2019/11/18
 * @Version 1.0
 **/
@Service("testService")
public class TestService implements BaseService {
    @Autowired
    private IJpaDao<User> userDao;
    @Autowired
    private UserVoConvert convert;

    public DefaultServiceResult<UserVo> findUserByName(DefaultServiceParam<String> param){
        User user = new User();
        user.setUsername(param.getParam());
        List<User> datas = userDao.findBySenstive(user);
        if (!CollectionUtils.isEmpty(datas)){
            return DefaultServiceResult.build(convert.toDto(datas.get(0)));
        }
        return null;
    }

    public ServiceResultList<UserVo> findUsers(DefaultServiceParam<UserQueryVo> query){
        List<User> datas = userDao.queryByCriteria(query.getParam(),null);
        if (!CollectionUtils.isEmpty(datas)){
            return new ServiceResultList(convert.toDto(datas));
        }
        return new ServiceResultList(Collections.EMPTY_LIST);
    }
}
